package com.school.aftersalemanagement.controller.ui.main;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.school.aftersalemanagement.R;

import com.school.aftersalemanagement.controller.ui.bicyclemanagement.BicycleManagementActivity;
import com.school.aftersalemanagement.controller.ui.maintenancerecord.MaintenanceRecordActivity;
import com.school.aftersalemanagement.controller.ui.maintenanceremind.MaintenanceRemindActivity;
import com.school.aftersalemanagement.controller.ui.register.RegisterActivity;
import com.school.aftersalemanagement.controller.ui.set.SetActivity;
import com.school.aftersalemanagement.global.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by winkyle on 2017/10/3.
 */
public class MenuFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {
    private String Tag = "MenuFragment";
    @BindView(R.id.main_menu_username)
    TextView username;
    @BindView(R.id.main_menu_status)
    TextView status;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_menu, container, false);
        NavigationView navigationView = (NavigationView) view.findViewById(R.id.main_menu);
        navigationView.setNavigationItemSelectedListener(this);
        ButterKnife.bind(this,navigationView.getHeaderView(0));
        return view;
    }

    @OnClick(R.id.main_menu_head)
    public void onViewClicked(View view){
        ((BaseActivity)getActivity()).startActivity(RegisterActivity.class);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
      if (((MainActivity) getActivity()).isAvailable()) {
            if (id == R.id.menu_bar_bicycle) {
                Intent intent = new Intent(getActivity(), BicycleManagementActivity.class);
                intent.putExtra("location", ((MapFragment)getFragmentManager().findFragmentById(R.id.fragment_main_map)).getLocation());
                getActivity().startActivityForResult(intent, 200);
            } else if (id == R.id.menu_bar_maintenanceRecord) {
                ((BaseActivity)getActivity()).startActivity(MaintenanceRecordActivity.class);
            } else if (id == R.id.menu_bar_maintenanceReminding) {
                Intent intent = new Intent(getActivity(),MaintenanceRemindActivity.class);
                intent.putExtra("","");
                getActivity().startActivityForResult(intent, 200);
            } else if (id == R.id.menu_bar_set) {
                ((BaseActivity)getActivity()).startActivity(SetActivity.class);
            }
        }
        return true;
    }

    public void refresh(){
        Log.d(Tag,"refresh");
        if(((MainActivity) getActivity()).getUser()!=null){
            username.setText(((MainActivity) getActivity()).getUser().getRname());
            status.setText("状态 : 在线");
        }else{
            username.setText("用户:无");
            status.setText("状态 : 离线");
        }
    }
}
